package com.xiaoq.shop.service.impl;

import com.xiaoq.shop.entity.SKUEntity;
import com.xiaoq.shop.entity.SPUEntity;
import com.xiaoq.shop.entity.SPUReviewEntity;
import com.xiaoq.shop.repository.ISPURepository;
import com.xiaoq.shop.service.ISPUService;
import com.xiaoq.shop.vo.SPUReviewScoreVo;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by chenjh on 2017/11/28.
 */
@Service
public class SPUServiceImpl implements ISPUService {

    @Autowired
    private ISPURepository spuRepository;

    @Override
    public boolean save(SPUEntity spuEntity) {
        return spuRepository.save(spuEntity);
    }

    @Override
    public SPUEntity getByShopIdAndSpuName(String shopId, String spuName) {
        if(StringUtils.isAnyBlank(shopId, spuName)){
            return null;
        }
        SKUEntity skuEntity = new SKUEntity();
        skuEntity.setShopId(shopId);
        skuEntity.setSpuName(spuName);
        return spuRepository.getSPUEntityByCommon(skuEntity);
    }

    @Override
    public SPUEntity getByShopIdAndSpuCode(String shopId, String spuCode) {
        if(StringUtils.isAnyBlank(shopId, spuCode)){
            return null;
        }
        SKUEntity skuEntity = new SKUEntity();
        skuEntity.setShopId(shopId);
        skuEntity.setSpuCode(spuCode);
        return spuRepository.getSPUEntityByCommon(skuEntity);
    }

    @Override
    public boolean updateById(SPUEntity spuEntity) {
        return spuRepository.updateById(spuEntity);
    }

    @Override
    public boolean deleteByShopIdAndSpuCode(String shopId, String spuCode) {
        return spuRepository.deleteByShopIdAndSpuCode(shopId, spuCode);
    }

    @Override
    public SPUReviewScoreVo getReviewScoreByShopIdAndCataCode(String shopId, String catelogCode) {
        if(StringUtils.isBlank(shopId)){
            return null;
        }
        List<SPUEntity> spuEntityList = spuRepository.getSPUByShopIdAndCatalogCode(shopId, catelogCode);
        if(CollectionUtils.isEmpty(spuEntityList)){
            return null;
        }
        SPUReviewScoreVo spuReviewScoreVo = new SPUReviewScoreVo();
        List<SPUReviewScoreVo.ScoreStat> scoreStatList = new ArrayList<>();
        Map<Integer, Integer> score2NumMap = new HashMap<>();

        for(SPUEntity spuEntity : spuEntityList){
            List<SPUReviewEntity> spuReviewEntityList = spuEntity.getSpuReviews();
            if(CollectionUtils.isNotEmpty(spuReviewEntityList)){
                for (SPUReviewEntity spuReviewEntity : spuReviewEntityList){
                    int score = spuReviewEntity.getScore();
                    if(score2NumMap.containsKey(score)){
                        score2NumMap.put(score, score2NumMap.get(score)+1);
                    }else {
                        score2NumMap.put(score, 1);
                    }
                }
            }
        }

        if(score2NumMap.size() > 0){
            score2NumMap.forEach((k, v)->{
                SPUReviewScoreVo.ScoreStat scoreStat = new SPUReviewScoreVo.ScoreStat();
                scoreStat.setNum(v);
                scoreStat.setScore(k);
                spuReviewScoreVo.setTotalNum(spuReviewScoreVo.getTotalNum() + v);
                spuReviewScoreVo.setTotalScore(spuReviewScoreVo.getTotalScore() + k*v);
                scoreStatList.add(scoreStat);
            });
        }
        spuReviewScoreVo.setScoreStatList(scoreStatList);
        if(spuReviewScoreVo.getTotalNum() > 0){
            spuReviewScoreVo.setAvgScore(1.0 * spuReviewScoreVo.getTotalScore()/spuReviewScoreVo.getTotalNum());
        }
        return spuReviewScoreVo;
    }

    @Override
    public Map<String, Double> getReviewScore4InterByShopIdAndCataCode(String shopId, String catalogCode) {
        Map<String,Double> resultMap = new HashMap<>();
        if(StringUtils.isBlank(shopId)){
            return resultMap;
        }
        List<SPUEntity> spuEntityList = spuRepository.getSPUByShopIdAndCatalogCode(shopId, catalogCode);
        if(CollectionUtils.isEmpty(spuEntityList)){
            return resultMap;
        }

        for(SPUEntity spuEntity : spuEntityList){
            int reviewNum = 0;
            double reviewScore = 0D;
            resultMap.put(spuEntity.getId(),0D); //讲解员的评分
            List<SPUReviewEntity> spuReviewEntityList = spuEntity.getSpuReviews();
            if(CollectionUtils.isNotEmpty(spuReviewEntityList)){
                for (SPUReviewEntity spuReviewEntity : spuReviewEntityList){
                    reviewScore += spuReviewEntity.getScore();
                    reviewNum ++;
                }
            }
            double avgScore = (reviewNum>0) ? reviewScore/reviewNum : 0;
            resultMap.put(spuEntity.getInterpreterId(), avgScore);
        }
        return resultMap;
    }

    @Override
    public List<SPUEntity> getSPUListByShopIdAndCataCode(String shopId, String catalogCode) {
        if(StringUtils.isBlank(shopId)){
            return new ArrayList<>();
        }
        return spuRepository.getSPUByShopIdAndCatalogCode(shopId, catalogCode);
    }

    @Override
    public boolean deleteByShopIdAndSpucodeList(String shopId, List<String> spuCodeList) {
        return spuRepository.deleteByShopIdAndSpucodeList(shopId, spuCodeList);
    }
}
